#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>
#ifdef USE_SDL
	#include <ClanLib/sdl.h>
#else
	#include <ClanLib/gl.h>
#endif
#include "hatGuy.h"

HatGuy::HatGuy()
{
}

HatGuy::HatGuy(CL_Sprite &stand_sprite, CL_Sprite &move_sprite, CL_Sprite &jump_sprite, CL_Sprite &land_sprite, CL_Sprite &attack_sprite, bool &attack_key, bool &jump_key, bool &left_key, bool &right_key)
: Entity(stand_sprite)
{
	vel = CL_Vector2(0.0f, 0.0f);
	accel = CL_Vector2(0.0f, 300.0f);

	action = HATGUY_STANDING;
	direction = HATGUY_RIGHT;

	walkSpeed = 75.0f;
	airSpeed = 70.0f;
	airCoefficient = 5.0f;

	stand = &stand_sprite;
	move = &move_sprite;
	jump = &jump_sprite;
	land = &land_sprite;
	attack = &attack_sprite;

	key_attack = &attack_key;
	key_jump = &jump_key;
	key_left = &left_key;
	key_right = &right_key;
}

HatGuy::~HatGuy()
{
}

inline void HatGuy::logic(float &time)
{
	switch(action)
	{
	case HATGUY_STANDING:
		if(*key_attack)
		{
			sprite->set_image_data(*attack);
			sprite->set_frame(0);
			action = HATGUY_ATTACKING;
		}
		else if(*key_jump && onGround)
		{
			sprite->set_image_data(*jump);
			sprite->set_frame(0);
			action = HATGUY_JUMPING;
		}
		else if(*key_left && onGround)
		{
			sprite->set_image_data(*move);
			sprite->set_frame(0);
			action = HATGUY_WALKING;
			if(direction != HATGUY_LEFT)
			{
				sprite->rotate_yaw(-180);
				direction = HATGUY_LEFT;
			}
		}
		else if(*key_right && onGround)
		{
			sprite->set_image_data(*move);
			sprite->set_frame(0);
			action = HATGUY_WALKING;
			if(direction != HATGUY_RIGHT)
			{
				sprite->rotate_yaw(180);
				direction = HATGUY_RIGHT;
			}
		}
		break;
	case HATGUY_ATTACKING:
		if(sprite->is_finished())
		{
			sprite->set_image_data(*stand);
			action = HATGUY_STANDING;
		}
	case HATGUY_JUMPING:
		if(sprite->is_finished())
		{
		  vel.y = -200.0f;
			if(direction == HATGUY_LEFT) {
			  //vel.x *= -1;
			}
			action = HATGUY_INAIR;
			
		}
		break;
	case HATGUY_INAIR:
		if(onGround)
		{
		  vel.x = 0;
		  accel.x = 0;
			sprite->set_image_data(*land);
			sprite->set_frame(0);
			action = HATGUY_LANDING;
		} else {
		  if (*key_left) {
		    accel.x = -1*airCoefficient*(airSpeed+vel.x);
		  } else if(*key_right) {
		    accel.x = airCoefficient*(airSpeed-vel.x);
		  } else {
		    accel.x = -1*vel.x;
		  }
		}
		break;
	case HATGUY_LANDING:
		if(sprite->is_finished())
		{
			sprite->set_image_data(*stand);
			action = HATGUY_STANDING;
		}
		break;
	case HATGUY_WALKING:
		if(direction == HATGUY_LEFT && *key_left)
		{
			pos.x -= (walkSpeed * time);
			if (*key_jump) {
			  sprite->set_image_data(*jump);
			  sprite->set_frame(0);
			  //vel.x = -1 * airSpeed;
			  action = HATGUY_JUMPING;
			}
		}
		else if(direction == HATGUY_RIGHT && *key_right)
		{
			pos.x += (walkSpeed * time);
			if (*key_jump) {
			  sprite->set_image_data(*jump);
			  sprite->set_frame(0);
			  //vel.x = airSpeed; 
			  action = HATGUY_JUMPING;
			}
		}
		else
		{
			sprite->set_image_data(*stand);
			action = HATGUY_STANDING;
		}
		break;
	}
}
